<?php

namespace App\Console\Commands;

use App\Models\BookOrder;
use App\Models\BookOrderReview;
use App\Models\BookRule;
use Illuminate\Console\Command;

class OrderToMaking extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order:making';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单转制作中';

    public function handle()
    {
        $orders = BookOrder::whereHas('rule', function ($query) {
            $query->where('eat_date', now()->addDay(1)->format("Y-m-d"));
        })->where('status', BookOrder::ORDER_STATUS_INIT)->with(['school.rule', 'rule'])->get();
        foreach ($orders as $order) {
            if ($order->type === BookOrder::TYPE_OFFICIAL && BookOrderReview::where('order_id', $order->id)->first()->status != BookOrder::REVIEWED) {
                continue;
            }
            $current = date("H:i:s");
            switch ($order->rule->eat_type){
                case BookRule::EAT_TYPE_BREAKFAST:
                    if($current >= $order->school->rule->morning_deadline) {
                        $order->making();
                    }
                    break;
                case BookRule::EAT_TYPE_LUNCH:
                    if($current >= $order->school->rule->lunch_deadline) {
                        $order->making();
                    }
                    break;
                case BookRule::EAT_TYPE_DINNER:
                    if($current >= $order->school->rule->dinner_deadline) {
                        $order->making();
                    }
                    break;
            }
        }
    }

}
